package com.ynyr.jar.wss;

import com.corundumstudio.socketio.SocketIOServer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

import javax.annotation.PreDestroy;

@Slf4j
@SpringBootApplication
@ComponentScan(basePackages = {"com.ynyr.jar.wss"})
public class Application implements CommandLineRunner {

    private final SocketIOServer socketIOServer;

    public Application(SocketIOServer socketIOServer) {
        this.socketIOServer = socketIOServer;
    }

    @Override
    public void run(String... args) throws Exception {
        socketIOServer.start();
    }

    @PreDestroy
    public void exitCodeGenerator() {
            try {
                socketIOServer.stop();
            } catch (Exception e) {
                log.error(ExceptionUtils.getStackTrace(e));
            }
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
